package com.pactera.madp.cp.controller.oms.channel;

import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.channel.ChannelCodeGroupListVO;
import com.pactera.madp.cp.api.dto.channel.ChannelCodeGroupMoveDTO;
import com.pactera.madp.cp.api.dto.channel.ReqChannelCodeGroupAddDTO;
import com.pactera.madp.cp.api.dto.channel.ReqChannelCodeGroupUpdateDTO;
import com.pactera.madp.cp.api.entity.channel.ChannelCodeGroupEntity;
import com.pactera.madp.cp.service.IChannelCodeGroupService;
import com.pactera.madp.cp.service.IChannelCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author: yangpengwei
 * @time: 2021/2/22 2:47 下午
 * @description 渠道码 - 分组
 */
@RestController
@AllArgsConstructor
@RequestMapping("/channelcodegroup")
@Api(value = "channelcodegroup", tags = "渠道码-分组")
public class ChannelCodeGroupController {

    private IChannelCodeGroupService channelCodeGroupService;
    private IChannelCodeService channelCodeService;

    /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 新建渠道码分组
     */
    @SysLog("新建渠道码分组")
    @PostMapping
    @ApiOperation(value = "新建渠道码分组")
    public R storeCodeGroup(@Validated @RequestBody ReqChannelCodeGroupAddDTO dto) {
        channelCodeGroupService.saveByNames(dto);
        return R.ok();
    }

    /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 编辑渠道码分组
     */
    @SysLog("编辑渠道码分组")
    @PutMapping
    @ApiOperation(value = "编辑渠道码分组")
    public R updateCodeGroup(@Validated @RequestBody ReqChannelCodeGroupUpdateDTO dto) {
        channelCodeGroupService.updateNameByGroupId(dto);
        return R.ok();
    }

    /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 查询渠道码分组
     */
    @GetMapping("/index")
    @ApiOperation(value = "查询渠道码分组列表")
    public R<List<ChannelCodeGroupListVO>> showCodeGroupList() {
        return R.ok(channelCodeGroupService.codeGroupList());
    }

    /**
     * @author: yangpengwei
     * @time: 2021/2/22 3:53 下午
     * @description 渠道码分组详情
     */
    @GetMapping("/{groupId}")
    @ApiOperation(value = "渠道码分组详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="分组id",dataType = "Integer")
    })
    public R showCodeGroupDetail(@PathVariable Integer groupId) {
        ChannelCodeGroupEntity entity = channelCodeGroupService.getById(groupId);
        if (Objects.isNull(entity)) {
            throw new CheckedException("渠道码分组 id 不存在");
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("groupId", entity.getId());
        map.put("name", entity.getName());
        return R.ok(map);
    }

    /**
     * @author: yangpengwei
     * @time: 2021/2/23 9:39 上午
     * @param channelCodeId 现分组，groupId 移动后分组
     * @description
     */
    @SysLog("移动渠道码分组")
    @PutMapping("/move")
    @ApiOperation(value = "移动渠道码分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name="groupId",value="新分组id",dataType = "Integer"),
            @ApiImplicitParam(name="channelCodeId",value="渠道码id",dataType = "Integer")
    })
    public R showCodeGroupMove(Integer channelCodeId, Integer groupId) {
        if (channelCodeId <= 0) {
            throw new CheckedException("channelCodeId 不能为空");
        }
        if (groupId <= 0) {
            throw new CheckedException("groupId 不能为空");
        }
        channelCodeService.updateGroupId(channelCodeId, groupId);
        return R.ok();
    }

    @SysLog("删除渠道活码分组")
    @DeleteMapping
    @ApiOperation(value = "删除渠道活码分组")
    public R<Boolean> deleteCodeGroup(Integer id) {
        return channelCodeGroupService.deleteCodeGroup(id) ? R.ok(Boolean.TRUE, "删除成功") : R.failed(Boolean.FALSE, "删除失败");
    }

    @SysLog("批量移动渠道码分组")
    @PutMapping("/moves")
    @ApiOperation(value = "批量移动渠道码分组")
    public R showCodeGroups(@RequestBody ChannelCodeGroupMoveDTO dto) {
        channelCodeService.updateGroupId(dto);
        return R.ok();
    }

}
